//
//  VistorView.swift
//  0729FirstProgramMicroblog
//
//  Created by ding on 15/7/29.
//  Copyright © 2015年 ding. All rights reserved.
//

import UIKit

    // MARK: - 登陆注册按钮协议
@objc protocol VistorViewDelegate: NSObjectProtocol {
    
    /// 登陆
    func VistorViewLogin()
    /// 注册
    optional func VistorViewRegister()
    
}

//    //MARK: - 定义代理属性
//weak var delegate: VistorViewDelegate?


class VistorView: UIView {
    
    //MARK: - 定义代理属性
    weak var delegate: VistorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupVistorUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
//        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置访客模式下各个控制器的显示
    func setupViewInfo(isHome: Bool, message: String, imageName: String) {
        smallIconView.image = UIImage(named: imageName)
        discLabel.text = message
        houseView.hidden = !isHome
        isHome ? houseViewAnimation() : sendSubviewToBack(maskIconView)
    }
    
    /// houseView 的动画
    private func houseViewAnimation() {
        /// 基本动画
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2 * M_PI
        ani.duration = 22
        ani.repeatCount = MAXFLOAT
        ani.removedOnCompletion = false
        smallIconView.layer.addAnimation(ani, forKey: nil)
    }
    
    func setupVistorUI() {
        // 向View中添加各种子视图（懒加载的方式）
        addSubview(smallIconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(discLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        // 设置约束
        
        smallIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: smallIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: smallIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -60))
        
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[ss]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["ss" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[ss]-(-31)-[loginBtn]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["ss" : maskIconView, "loginBtn" :  loginBtn]))
        
        houseView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: smallIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: smallIconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        discLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: discLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: smallIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: discLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: smallIconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: discLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 250))
        
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: discLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: discLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: discLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: discLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
    
    /// 懒加载smallIcon
    private lazy var smallIconView : UIImageView = {
       let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return imgView
    }()
    
    /// 懒加载遮罩视图
    private lazy var maskIconView : UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return imgView
        }()
    
    /// 懒加载house
    private lazy var houseView : UIImageView = {
        let imgView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return imgView
        }()
    
    /// 懒加载discLabel
    private lazy var discLabel : UILabel = {
        let discLabel = UILabel()
        discLabel.textColor = UIColor.darkGrayColor()
        discLabel.font = UIFont.systemFontOfSize(14)
        discLabel.numberOfLines = 0
//        discLabel.text = "如果上天能够给我一个再来一次的机会，我会对那个女孩子说三个字：我爱你"
        discLabel.sizeToFit()
        discLabel.textAlignment = NSTextAlignment.Center
        return discLabel
        }()
    
    /// 登陆按钮
    private lazy var loginBtn : UIButton = {
        let login = UIButton()
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        login.setTitle("登陆", forState: UIControlState.Normal)
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        login.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        return login
        }()
    
    /// 注册按钮
    private lazy var registerBtn : UIButton = {
        let register = UIButton()
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        register.setTitle("注册", forState: UIControlState.Normal)
        register.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        register.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        return register
        }()
    
    func loginClick() {
        delegate?.VistorViewLogin()
    }
    
    func registerClick() {
        delegate?.VistorViewRegister!()
    }

}
